<template>
	<div class="toast" v-show="show">{{text}}</div>
</template>

<script>
	import { defineComponent, reactive, toRefs } from 'vue'

	export default defineComponent({
		name: 'Toast',
		props: {
			show: {
				type: Boolean,
				default: false
			},
			text: {
				type: String,
				default: ''
			}
		}
	})

	export const useToastEffect = () => {
		const toastData = reactive({
			show: false,
			text: ''
		})
		const showToast = (message) => {
			toastData.show = true
            toastData.text = message
            setTimeout(() => {
              	toastData.show = false
              	toastData.text = ''
            }, 2e3)
		}
		const { show, text } = toRefs(toastData)
		return {
			show,
			text,
			showToast
		}
	}
</script>

<style lang="scss" scoped>
	.toast {
		position: fixed;
		left: 50%;
		top: 50%;
		z-index: 100;
		transform: translate(-50%, -50%);
		padding: .1rem;
		background: rgba(0, 0, 0, .35);
		border-radius: .05rem;
		color: #fff;
	}
</style>
